<?php
namespace app\job\service;

use app\model\CrisisLogModel;
use app\model\DepartmentAdminModel;
use app\model\FollowUserModel;
use app\model\MessageModel;
use app\model\TaskModel;
use app\model\TaskRatingScaleModel;
use app\model\TaskUserResultModel;
use app\model\UserModel;
use app\model\WarningReportRecordModel;
use think\Service;

class RatingScaleEvaluationService extends Service
{
    /**
     * 量表测评-任务
     * @param $data
     * @return void
     */
    public function task($data): void
    {
        $TaskModel = new TaskModel();
        $TaskRatingScaleModel = new TaskRatingScaleModel();
        $TaskUserResultModel = new TaskUserResultModel();
        $UserModel = new UserModel();
        //任务记录
        $taskInfo = $TaskModel->find($data['task_id']);
        //任务量表记录
        $query = $TaskRatingScaleModel->where([
            'task_id' => $data['task_id'],
            'rating_scale_id' => $data['rating_scale_id']
        ])->inc('user_complete_num');
        if ($data['is_warning'] == 1){
            $query->inc('warning_num');
            $taskInfo->warning_num = $TaskUserResultModel->where([
                ['task_id', '=', $data['task_id']],
                ['is_warning', '=', 1]
            ])->distinct('uid')->count('uid');
        }
        //完成完成数量
        $taskInfo->user_complete_num = $TaskUserResultModel->where('task_id', '=', $data['task_id'])
            ->field('uid')
            ->group('uid')
            // 统计不同任务的完成次数（去重任务ID）
            ->having('count(id) = '.$taskInfo['rating_scale_count'])
            ->count();
        $taskInfo->user_complete_rate = number_format(($taskInfo->user_complete_num / $taskInfo->user_count) * 100, 2);
        $query->save();
        $taskInfo->save();

        $user_name = $UserModel->where('id', '=', $data['uid'])->value('name');
        if ($data['is_warning'] == 1) {
            $WarningReportRecordModel = new WarningReportRecordModel();
            $WarningReportRecordModel->insert([
                'unit_id' => $data['unit_id'],
                'uid' => $data['uid'],
                'rating_scale_id' => $data['rating_scale_id'],
                'source' => 1,
                'result_id' => $data['id'],
                'time' => time()
            ]);
            //加入关注人群
            $FollowUserModel = new FollowUserModel();
            $FollowUserModel->insert([
                'unit_id' => $data['unit_id'],
                'operator_uid' => $data['uid'],
                'uid' => $data['uid'],
                'channel' => 2,
                'channel_id' => $data['id'],
                'time' => time()
            ]);
            //消息记录盒子
            $CrisisLogModel = new CrisisLogModel();
            $CrisisLogModel->insert([
                'unit_id' => $data['unit_id'],
                'uid' => $data['uid'],
                'type' => 2,
                'source_type' => 1,
                'behavior' => '移入关注人群',
                'time' => time()
            ]);
            //系统消息
            $MessageData[] = [
                'uid' => $data['uid'],
                'type' => 6,
                'title' => '预警报告',
                'content' => $user_name . '用户发现预警，' . $taskInfo['name'] . '测评任务，已自动将该用户加入关注人群，请及时处理。',
                'time' => time()
            ];
        }
        //系统消息
        $MessageData[] = [
            'uid' => $data['uid'],
            'type' => 5,
            'title' => '测评报告',
            'content' => $user_name . '用户完成了' . $taskInfo['name'] . '测评任务，已生成测评报告，请及时查看。',
            'time' => time()
        ];
        $MessageModel = new MessageModel();
        $MessageModel->insertAll($MessageData);
    }

    /**
     * 量表测评-自我
     * @param $ResultData
     * @return void
     */
    public function self($ResultData): void
    {
        //处理预警
        if ($ResultData['is_warning'] == 1) {
            $WarningReportRecordModel = new WarningReportRecordModel();
            $WarningReportRecordModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'uid' => $ResultData['uid'],
                'rating_scale_id' => $ResultData['rating_scale_id'],
                'source' => 2,
                'result_id' => $ResultData['id'],
                'time' => time()
            ]);
            //加入关注人群
            $UserModel = new UserModel();
            $department_id = $UserModel->where('id', '=', $ResultData['uid'])->value('department_id');
            $DepartmentAdminModel = new DepartmentAdminModel();
            $operator_uid = $DepartmentAdminModel->where('department_id', '=', $department_id)->value('uid');
            if (!$operator_uid) {
                $operator_uid = $UserModel->where([
                    ['unit_id', '=', $ResultData['unit_id']],
                    ['role_id', '=', 0]
                ])->value('id');
            }
            $FollowUserModel = new FollowUserModel();
            $FollowUserModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'operator_uid' => $operator_uid,
                'uid' => $ResultData['uid'],
                'channel' => 3,
                'channel_id' => $ResultData['id'],
                'time' => time()
            ]);
            //消息记录盒子
            $CrisisLogModel = new CrisisLogModel();
            $CrisisLogModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'uid' => $ResultData['uid'],
                'type' => 2,
                'source_type' => 1,
                'behavior' => '移入关注人群',
                'time' => time()
            ]);
        }
    }

    /**
     * 量表测评-他人代测
     * @param $ResultData
     * @return void
     */
    public function proxy($ResultData): void
    {
        if ($ResultData['is_warning'] == 1) {
            $WarningReportRecordModel = new WarningReportRecordModel();
            $WarningReportRecordModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'uid' => $ResultData['uid'],
                'rating_scale_id' => $ResultData['rating_scale_id'],
                'source' => 3,
                'result_id' => $ResultData['id'],
                'time' => time()
            ]);
            //加入关注人群
            $UserModel = new UserModel();
            $department_id = $UserModel->where('id', '=', $ResultData['uid'])->value('department_id');
            $DepartmentAdminModel = new DepartmentAdminModel();
            $operator_uid = $DepartmentAdminModel->where('department_id', '=', $department_id)->value('uid');
            if (!$operator_uid) {
                $operator_uid = $UserModel->where([
                    ['unit_id', '=', $ResultData['unit_id']],
                    ['role_id', '=', 0]
                ])->value('id');
            }
            $FollowUserModel = new FollowUserModel();
            $FollowUserModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'operator_uid' => $operator_uid,
                'uid' => $ResultData['uid'],
                'channel' => 4,
                'channel_id' => $ResultData['id'],
                'time' => time()
            ]);
        }
    }

    public function wireless($ResultData): void
    {
        if ($ResultData['is_warning'] == 1) {
            $WarningReportRecordModel = new WarningReportRecordModel();
            $WarningReportRecordModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'uid' => $ResultData['uid'],
                'rating_scale_id' => $ResultData['rating_scale_id'],
                'source' => 4,
                'result_id' => $ResultData['id'],
                'time' => time()
            ]);
            //加入关注人群
            $UserModel = new UserModel();
            $department_id = $UserModel->where('id', '=', $ResultData['uid'])->value('department_id');
            $DepartmentAdminModel = new DepartmentAdminModel();
            $operator_uid = $DepartmentAdminModel->where('department_id', '=', $department_id)->value('uid');
            if (!$operator_uid) {
                $operator_uid = $UserModel->where([
                    ['unit_id', '=', $ResultData['unit_id']],
                    ['role_id', '=', 0]
                ])->value('id');
            }
            $FollowUserModel = new FollowUserModel();
            $FollowUserModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'operator_uid' => $operator_uid,
                'uid' => $ResultData['uid'],
                'channel' => 5,
                'channel_id' => $ResultData['id'],
                'time' => time()
            ]);
            //消息记录盒子
            $CrisisLogModel = new CrisisLogModel();
            $CrisisLogModel->insert([
                'unit_id' => $ResultData['unit_id'],
                'uid' => $ResultData['uid'],
                'type' => 2,
                'source_type' => 1,
                'behavior' => '移入关注人群',
                'time' => time()
            ]);
        }
    }

}
